// ---------------------------------------------------------------------
//
// -- 主应用数据
// -- author £
//
// ---------------------------------------------------------------------

const HexagonUtil = require('HexagonUtil'),
    CommonUtil = require('CommonUtil'),
    superClass = require('game_single_base').GameSingleModel;

let cl = cc.Class({
    extends: superClass,
    properties: {
        
    },

    initCtor: function () {
        let row, col, grid,
            startCol = 5, index = -1;

        this._grids = [];
        this._items =[];

        for (row = 0; row < 9; ++row) {
            if (row >= 1 && row <= 4) {
                ++startCol;
            } else if (row >= 5 && row <= 8) {
                --startCol;
            }

            for (col = 0; col < startCol; ++col) {
                ++index;
                grid = {};
                grid.free = true;
                grid.d = [];
                this._grids[index] = grid;
                /** ----------------------- */
                grid.d[0] = col < (startCol - 1) ? 
                    (index + 1) : null;
                /** ----------------------- */
                if (row >= 4 && row <= 7 && col === (startCol - 1) ||
                    row === 8) {
                    grid.d[1] = null;
                } else {
                    grid.d[1] = row <= 3 ? 
                        (index + startCol + 1) : (index + startCol);
                }
                /** ----------------------- */
                if (row >= 4 && row <= 7 && col === 0 ||
                    row === 8) {
                    grid.d[2] = null;
                } else {
                    grid.d[2] = row <= 3 ? 
                        (index + startCol) : (index + startCol - 1);
                }
                /** ----------------------- */
                grid.d[3] = col > 0 ? 
                    (index - 1) : null;
                /** ----------------------- */
                if (row >= 1 && row <= 4 && col === 0 ||
                    row === 0) {
                    grid.d[4] = null;
                } else {
                    grid.d[4] = row <= 4 ? 
                        (index - startCol) : (index - startCol - 1);
                }
                /** ----------------------- */
                if (row >= 1 && row <= 4 && col === (startCol - 1) ||
                    row === 0) {
                    grid.d[5] = null;
                } else {
                    grid.d[5] = row <= 4 ? 
                        (index - startCol + 1) : (index - startCol);
                }
            }
        }

        // console.log('_grids:', this._grids);
        
    },

    clearData: function () {
        this.selectIndex = -1;
        this.curIndexes = null;
    },

    resetData: function () {
        CommonUtil.mysuper(superClass, this, 'resetData'); 

        let i, len = this._grids.length;

        for (i = 0; i < len; ++i) {
            this._grids[i].free = true;
        }

        this._items.length = 0;
        this.selectIndex = -1;
    },

    revive: function () {
        let i, len = this._grids.length;

        for (i = 0; i < len; ++i) {
            this._grids[i].free = true;
        }
    },

    isGridFree: function (index) {
        return this._grids[index].free;
    },

    getGridColor: function (index) {
        return this._grids[index].colorIndex;
    },

    getItem: function (index) {
        return this._items[index];
    },

    selectData: function () {
        return this._items[this.selectIndex];
    },

    recover: function (data) {
        // let i, len = data.items.length;

        // for (i = 0; i < len; ++i) {
        //     data.items[i].color = 
        //         cc.hexToColor('#' + Math.floor((data.items[i].color._val) / 256).toString(16));
        // }

        this._grids = data.grids;
        this._items = data.items;
    },

    useLaser: function (data) {
        let i, len = data.length, curData, curD, 
            curIndex, indexes = [];

        for (i = 0; i < len; ++i) {
            curData = data[i];
            curD = curData.d;
            curIndex = curData.start;
            indexes[indexes.length] = curIndex;
            this._grids[curIndex].free = true;

            while(curIndex !== curData.end) {
                curIndex = this._grids[curIndex].d[curD];
                indexes[indexes.length] = curIndex;
                this._grids[curIndex].free = true;
            }
        }

        return indexes;
    },

    useHammer: function (index) {
        this._grids[index].free = true;
    },

    useBomb: function (index) {
        let bombData = HexagonUtil.getBombPath(),
            erases = [index], i, len = bombData.length,
            j, len2,
            curIndex, tmp;

        this._grids[index].free = true;

        for (i = 0; i < len; ++i) {
            len2 = bombData[i].length;
            curIndex = index;

            for (j = 0; j < len2; ++j) {
                curIndex = this._grids[curIndex].d[bombData[i][j]];

                if (curIndex === null) {
                    break;
                }
            }

            if (curIndex === null)
                continue;
                
            erases[erases.length] = curIndex;
            this._grids[curIndex].free = true;
        }

        return erases;
    },

    setGrids: function () {
        let i, len = this.curIndexes.length;

        for (i = 0; i < len; ++i) {
            this._grids[this.curIndexes[i]].free = false;
            this._grids[this.curIndexes[i]].colorIndex = this.getSelectColor();
        }
    },

    checkGrid: function (index, index2) {
        if (index2 === undefined || index2 === null)
            index2 = this.selectIndex;

        let selectData = this.getItem(index2),
            path = selectData.nodePath,
            curIndex,
            i, len = path.length,
            j, len2,
            result = [index];

        if (!this._grids[index].free) return null;

        for (i = 1; i < len; ++i) {
            len2 = path[i].length;
            curIndex = index;

            for (j = 0; j < len2; ++j) {
                curIndex = this._grids[curIndex].d[path[i][j]];

                if (curIndex === null || !this._grids[curIndex].free) {
                    return null;
                }
            }

            result[result.length] = curIndex;
        }

        return result;
    },

    checkItem: function (index) {
        let i = 0, len = this._grids.length;

        for (i = 0; i < len; ++i) {
            if (!this._grids[i].free) 
                continue;

            if (this.checkGrid(i, index)) {
                return true;
            }
        }

        return false;
    },  

    checkItems: function () {
        let result = [];
        
        for (let i = 0; i < 3; ++i) {
            result[i] = this.checkItem(i);
        }

        return result;
    },

    getEraseIndexes: function () {
        let i, len = this.curIndexes.length,
            j, k, l, len2, len3, interrupt, curIndex,
            result = null, tmpArr, diff;

        for (i = 0; i < len; ++i) {
            for (j = 0; j < 3; ++j) {
                interrupt = false;
                curIndex = this.curIndexes[i];
                tmpArr = [curIndex];

                while (curIndex !== null) {
                    curIndex = this._grids[curIndex].d[j];

                    if (curIndex !== null) tmpArr.push(curIndex);

                    if (curIndex !== null && this._grids[curIndex].free) {
                        interrupt = true;
                        break;
                    }
                }

                if (interrupt) continue;

                curIndex = this.curIndexes[i];

                while (curIndex !== null) {
                    curIndex = this._grids[curIndex].d[j + 3];

                    if (curIndex !== null) tmpArr.push(curIndex);

                    if (curIndex !== null && this._grids[curIndex].free) {
                        interrupt = true;
                        break;
                    }
                }

                if (interrupt) continue;

                if (!result) result = [];

                diff = 0;
                len2 = result.length
                len3 = tmpArr.length;

                for (k = 0; k < len2; ++k) {
                    for (l = 0; l < len3; ++l) {
                        if (result[k].indexOf(tmpArr[l]) < 0) {
                            ++diff;
                            break;
                        }
                    }
                }

                if (diff === result.length)
                    result.push(tmpArr);
            }
        }

        if (result) {
            len = result.length;

            for (i = 0; i < len; ++i) {
                result[i].sort(function (a, b) {
                    return a - b;
                }); 
            }

            result.sort(function (a, b) {
                return a[0] - b[0];
            });

            for (i = 0; i < len; ++i) {
                len2 = result[i].length;

                for (j = 0; j < len2; ++j) {
                    this._grids[result[i][j]].free = true;
                }
            }
        }

        this.curIndexes = null;
        // console.log(result);
        return result;
        // return null;
    },

    getSelectColor: function () {
        return this.selectData().colorIndex;
    },  

    createNewItem: function (fixStep) {
        this._items[this.selectIndex] = HexagonUtil.createItem(fixStep);
    },

    refreshItems: function (simple) {
        let i;

        for (i = 0; i < 3; ++i) {
            this._items[i] = HexagonUtil.createItem(simple ? i : null);
        }
    },

    StartGameS: function () {
        this.refreshItems();
    },

});

let CountCashModel = new cl();
module.exports = CountCashModel;
